package org.example.chat.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.example.chat.dao.FriendRelationRepository;
import org.example.chat.dao.UserRepository;
import org.example.chat.entity.FriendRelation;
import org.example.chat.entity.User;
import org.example.chat.service.IFriendRelationService;
import org.example.chat.utils.CommonUtils;
import org.example.chat.vo.ContactInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class FriendRelationServiceImpl extends BaseService implements IFriendRelationService {

    @Autowired
    private FriendRelationRepository friendRelationRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public List<ContactInfoVo> queryUserFriendList(String username) {
        List<ContactInfoVo> data = new ArrayList<>();
        // 1.查询好友id和备注
        List<Object[]> res = friendRelationRepository.findFriendIdsByUserId(username);
        if (res == null || res.size() == 0) {
            return data;
        }
        // 2. 把好友按好友备注或昵称（备注优先）首字母分组
        String id = null;
        String note = null;
        User user = null;
        ContactInfoVo contactInfoVo = null;
        String pinyin = null;
        for (Object[] re : res) {
            id = (String) re[0];
            note = (String) re[1];
            // 根据id集合查询好友信息
            user = userRepository.findByUsername(id);
            if (user == null) {
                continue;
            }
            contactInfoVo = new ContactInfoVo();
            contactInfoVo.setFriendId(user.getId());
            contactInfoVo.setUsername(username);
            contactInfoVo.setFriendUsername(user.getUsername());
            contactInfoVo.setFriendNickname(user.getNickname());
            contactInfoVo.setFriendFaceImg(user.getFaceImg());
            contactInfoVo.setFriendEmail(user.getEmail());
            contactInfoVo.setFriendSex(user.getSex());
            contactInfoVo.setFriendPersonalizedSignature(user.getPersonalizedSignature());
            contactInfoVo.setFriendAreaName(user.getAreaName());
            contactInfoVo.setFriendNote(note);
            // 获取好友备注或昵称首字母（备注优先）
            if (StringUtils.isNotEmpty(note)) {
                pinyin = CommonUtils.convertPinyin(note);
            } else {
                pinyin = CommonUtils.convertPinyin(user.getNickname());
            }
            String initial = pinyin.substring(0, 1).toUpperCase();
            if (initial.matches("[A-Z]")) {
                contactInfoVo.setFriendInitial(initial);
            } else {
                contactInfoVo.setFriendInitial("#");
            }
            data.add(contactInfoVo);
        }
        return data;
    }

    @Override
    public int addRelation(FriendRelation relation) {
        // 1. 判断是否已经是好友关系
        List<Object[]> relations = friendRelationRepository.findRelation(relation.getUserId(), relation.getFriendId());
        if (relations != null && relations.size() > 0) {
            return 2;
        }
        // 2. 添加好友
        relation.setId(idGenerate.nextId("friendrelation"));
        relation.setCreateDate(LocalDate.now());
        FriendRelation res = friendRelationRepository.save(relation);
        if (res == null) {
            return 1;
        }
        return 0;
    }
}
